package edu.uoc.tdp.pac4.client.logandreg;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;

import edu.uoc.tdp.pac4.beans.Attendance;
import edu.uoc.tdp.pac4.beans.Group;
import edu.uoc.tdp.pac4.beans.Registration;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;

public class AttDataHelper {

	public List<AttendanceData> getAttendaces(ClientConnectionHelper connectionHelper_){	
		List<Registration> regList = new ArrayList<Registration>();
		List<Attendance> attList = new ArrayList<Attendance>();
		try{
			regList = (List<Registration>) connectionHelper_.getLogAndRegManager().getAllAcceptedRegByStudent(ClientConnectionHelper.getLoggedUser().getNIF());
			attList = (List<Attendance>) connectionHelper_.getLogAndRegManager().getAttendancesByStudent(regList);
 		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		} 
		return setAttendanceData(attList, connectionHelper_);	
	}

	
	/**
	 * Method to fill the AttendanceData
	 * @param regs
	 * @param connectionHelper_
	 * @return list of resultant AttendanceData
	 */
	public List<AttendanceData> setAttendanceData(List<Attendance> atts, ClientConnectionHelper connectionHelper_){
		
		List<AttendanceData> attDataList = new ArrayList<AttendanceData>();
		
		try{
			
			for (Iterator<Attendance> it = atts.iterator() ; it.hasNext(); ) {
				Attendance a = it.next();
				Group group = connectionHelper_.getAcademicManager().getGroup(a.getGroupid());		
				AttendanceData attData = new AttendanceData(group.getName(),a.getAttendanceDate(), a.getStartTime(), a.getAttendanceId());
				attDataList.add(attData);
			}
			
			
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(null, 
					LanguageUtils.getMessage("client.error.message.remote"),
					LanguageUtils.getMessage("client.error.title"),
					JOptionPane.ERROR_MESSAGE);
		} 

		return attDataList;
	}
}
